package com.example.springboot.controller;

import com.example.springboot.entity.CollectIncorrectQuestion;
import com.example.springboot.entity.Res;
import com.example.springboot.entity.vo.IncorrectQuestionVo;
import com.example.springboot.service.Impl.CollectIncorrectQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/collectIncorrect")
public class CollectIncorrectQuestionController {

    @Autowired
    private CollectIncorrectQuestionService collectIncorrectQuestionService;
    @PostMapping("/add")
    public Res<Integer> addCollect(@RequestBody Map<String, Integer> params) {
        Integer userId = params.get("userId");
        Integer incorrectQuestionId = params.get("incorrectQuestionId");
        Integer res =  collectIncorrectQuestionService.addCollect(userId, incorrectQuestionId);
        return new Res<Integer>().code(200).msg("收藏成功").data(res);
    }

    @GetMapping("/{id}")
    public Res<CollectIncorrectQuestion> getCollectById(@PathVariable Integer id) {
        CollectIncorrectQuestion collect = collectIncorrectQuestionService.getCollectById(id);
        if (collect != null) {
            return new Res<CollectIncorrectQuestion>().code(200).msg("查询成功").data(collect);
        } else {
            return new Res<CollectIncorrectQuestion>().code(404).msg("收藏记录未找到");
        }
    }

    @DeleteMapping("/{id}")
    public Res<Void> removeCollect(@PathVariable Integer id) {
        collectIncorrectQuestionService.removeCollect(id);
        return new Res<Void>().code(200).msg("删除成功");
    }

    @GetMapping("/user/{userId}")
    public Res<List<IncorrectQuestionVo>> getCollectsByUserId(@PathVariable Integer userId,
                                                                   @RequestParam(defaultValue = "1") Integer pageNum,
                                                                   @RequestParam(defaultValue = "10") Integer pageSize) {
        List<IncorrectQuestionVo> collects = collectIncorrectQuestionService.getCollectsByUserId(userId, pageNum, pageSize);
        if (collects.isEmpty()) {
            return new Res<List<IncorrectQuestionVo>>().code(404).msg("没有找到收藏记录");
        }
        return new Res<List<IncorrectQuestionVo>>().code(200).msg("查询成功").data(collects);
    }
}